




package MagnetoProject;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MagnetoMeter extends javax.swing.JFrame {

        final static int MAIN_WINDOW_HEIGHT = 530;
        final static int MAIN_WINDOW_WIDTH = 750;
        static boolean START = false;
        
         JButton stopButton = new JButton("Stop");
         
        
        private List _listeners = new ArrayList();
      //  Buffer buffer = new Buffer();
         static Serialcomm comm = new Serialcomm();


      //  Storage store = new Storage();
      static  DisplayArea displayArea = new DisplayArea();
       // static Thread display = new Thread ( displayArea );


        public static void main(String args[]) {

             DisplayArea displayArea = new DisplayArea();
             new Thread(displayArea).start();
            //Thread display = new Thread ( displayArea );

             //new Thread(displayArea).start();
            comm.start();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MagnetoMeter().setVisible(true);
                
           }
        });


    }

    /** Creates new form Magnetometer */
    public MagnetoMeter() {
        initComponents();
    }

    //this will initialize all the components on the main window
    private void initComponents(){

        //first lets set up the top level container
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setMinimumSize(new java.awt.Dimension(MAIN_WINDOW_WIDTH,MAIN_WINDOW_HEIGHT));

        //making the label next...
        JLabel welcomeLabel = new JLabel();
        welcomeLabel.setFont(new java.awt.Font("Batang", 0, 14));
        welcomeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        welcomeLabel.setText("Welcome to the Magnetometer Control Panel");
        welcomeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        
        //lets create all the buttons
        JButton startButton = new JButton("Start");
         startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        JButton demoButton = new JButton("Demo");
        demoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                demoButtonActionPerformed(evt);
            }
        });

       // JButton stopButton = new JButton("Stop");*/
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        }); 

        //next is the menu and menu items

        JMenuBar mainMenuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");

        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });

        JMenuItem helpMenuItem = new JMenuItem("User Manual");
        helpMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpMenuItemActionPerformed(evt);
            }
        });

        mainMenuBar.add(fileMenu);
        mainMenuBar.add(helpMenu);
        fileMenu.add(exitMenuItem);
        helpMenu.add(helpMenuItem);

        setJMenuBar(mainMenuBar);

        //now we will make the necessary panels
       // DisplayArea displayArea = new DisplayArea();

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        buttonPanel.add(demoButton);
        buttonPanel.add(Box.createHorizontalGlue());

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel,BoxLayout.Y_AXIS));
        bottomPanel.add(Box.createRigidArea(new Dimension(0,10)));
        bottomPanel.add(buttonPanel);
        bottomPanel.add(Box.createRigidArea(new Dimension(0,10)));

        JPanel welcomePanel = new JPanel();
        welcomePanel.setLayout(new BoxLayout(welcomePanel, BoxLayout.X_AXIS));
        welcomePanel.add(Box.createHorizontalGlue());
        welcomePanel.add(welcomeLabel);
        welcomePanel.add(Box.createHorizontalGlue());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        topPanel.add(Box.createRigidArea(new Dimension(0,15)));
        topPanel.add(welcomePanel);
        topPanel.add(Box.createRigidArea(new Dimension(0,15)));
        

        JPanel eastPanel = new JPanel();
        eastPanel.setPreferredSize(new Dimension(40,0));

        JPanel westPanel = new JPanel();
        westPanel.setPreferredSize(new Dimension(40,0));


        //lets put it all together!
        add(bottomPanel, "South");
        add(topPanel, "North");
        add(eastPanel, "East");
        add(westPanel, "West");
        add(displayArea, "Center");

        
    }

    //action events
        private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {


       
        createInputTestWindow();


       // START = true;
        //comm.setStart(true);
        
        //while(START){
            
           //displayArea.dataReceived();
            //displayArea.setData(true);
            
       //    stopButton.addActionListener(new java.awt.event.ActionListener() {
        //    public void actionPerformed(java.awt.event.ActionEvent evt) {
       //         stopButtonActionPerformed(evt);
          //  }
    //    });
            
           
            
            
        }





        

        private void demoButtonActionPerformed(java.awt.event.ActionEvent evt) {

        createAndShowDemo();
        }

        private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {

        System.exit(0);
        }

        private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
        
            comm.setStart(false);
            START = false;
            System.out.println("Stop Button: Stop what you're doing!");
        }

        private void helpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {

        System.out.println("This should give an easy to read user manual");
        }



    //custom methods
        private void createInputTestWindow(){
          //create a new Frame, and populate it with the correct components

       //Create the frame
       JFrame manualInputFrame = new JFrame("Manual Input Test");
       manualInputFrame.setMinimumSize(new Dimension(250,175));
       manualInputFrame.setResizable(false);
       manualInputFrame.setVisible(true);

         //Create the Components
       JButton enterButton = new JButton("Enter");
       JLabel xCoordLabel = new JLabel("X co-ord: ");
       JLabel yCoordLabel = new JLabel("Y co-ord: ");
       JLabel magnitudeLabel = new JLabel("Magnitude: ");
       final JTextField yCoordTextField = new JTextField();
       final JTextField xCoordTextField = new JTextField();
       final JTextField magnitudeTextField = new JTextField();
       //inputTextField.setColumns(10);

       JPanel backgroundPanel = new JPanel();
       backgroundPanel.setLayout(new BoxLayout(backgroundPanel, BoxLayout.Y_AXIS));

       JPanel topPanel = new JPanel();
       topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
       topPanel.add(Box.createRigidArea(new Dimension(10,0)));
       topPanel.add(xCoordLabel);
       topPanel.add(Box.createRigidArea(new Dimension(4,0)));
       topPanel.add(xCoordTextField);
       topPanel.add(Box.createHorizontalGlue());
       topPanel.add(Box.createRigidArea(new Dimension(24,0)));

       JPanel yCoordPanel = new JPanel();
       yCoordPanel.setLayout(new BoxLayout(yCoordPanel, BoxLayout.X_AXIS));
       yCoordPanel.add(Box.createRigidArea(new Dimension(10,0)));
       yCoordPanel.add(yCoordLabel);
       yCoordPanel.add(Box.createRigidArea(new Dimension(4,0)));
       yCoordPanel.add(yCoordTextField);
       yCoordPanel.add(Box.createHorizontalGlue());
       yCoordPanel.add(Box.createRigidArea(new Dimension(24,0)));

       JPanel magnitudePanel = new JPanel();
       magnitudePanel.setLayout(new BoxLayout(magnitudePanel, BoxLayout.X_AXIS));
       magnitudePanel.add(Box.createRigidArea(new Dimension(10,0)));
       magnitudePanel.add(magnitudeLabel);
       magnitudePanel.add(Box.createRigidArea(new Dimension(4,0)));
       magnitudePanel.add(magnitudeTextField);
       magnitudePanel.add(Box.createHorizontalGlue());
       magnitudePanel.add(Box.createRigidArea(new Dimension(24,0)));

       JPanel bottomPanel = new JPanel();
       bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
       bottomPanel.add(Box.createHorizontalGlue());
       bottomPanel.add(Box.createRigidArea(new Dimension(1,0)));
       bottomPanel.add(enterButton);
       bottomPanel.add(Box.createHorizontalGlue());

       //add the components
       manualInputFrame.add(backgroundPanel);
       backgroundPanel.add(Box.createRigidArea(new Dimension(0,10)));
       backgroundPanel.add(topPanel);
       backgroundPanel.add(Box.createRigidArea(new Dimension(0,2)));
       backgroundPanel.add(yCoordPanel);
       backgroundPanel.add(Box.createRigidArea(new Dimension(0,2)));
       backgroundPanel.add(magnitudePanel);
       backgroundPanel.add(Box.createRigidArea(new Dimension(0,2)));
       backgroundPanel.add(bottomPanel);
       backgroundPanel.add(Box.createRigidArea(new Dimension(0,35)));
       backgroundPanel.add(Box.createVerticalGlue());

       //now we need to add in the action listeners
       enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                String xInput = xCoordTextField.getText();
                String yInput = yCoordTextField.getText();
                String magnitudeInput = magnitudeTextField.getText();

                enterButtonActionPerformed(evt,xInput,yInput,magnitudeInput);

                }
       });
        }

        private void enterButtonActionPerformed(java.awt.event.ActionEvent evt, String xInput, String yInput, String magnitudeInput) {

        System.out.println("Your inputs were ");
        System.out.println("X input: " + xInput);
        System.out.println("Y input: " + yInput);
        System.out.println("Magnitude: " + magnitudeInput);

        System.out.println("Passing Along your Inputs...");

        int[] inputs = new int[3];

        inputs[0] = Integer.parseInt(xInput);
        inputs[1] = Integer.parseInt(yInput);
        inputs[2] = Integer.parseInt(magnitudeInput);

        Buffer.enter(inputs);

        }

    public synchronized void addStorageListener( StorageListener l ) {
        _listeners.add( l );
    }

        public synchronized void fireStorageEvent() {
        StorageEvent storage = new StorageEvent( this );
        Iterator listeners = _listeners.iterator();
        while( listeners.hasNext() ) {
            //( (StorageListener) listeners.next() ).dataReceived( storage );
            ( (StorageListener) listeners.next() ).something( storage );
        }
    }
  

        
        private static void createAndShowDemo(){

        JButton clearButton = new JButton("Clear");
        JPanel southpanel = new JPanel();
        JPanel northpanel = new JPanel();
        JPanel eastpanel = new JPanel();
        JPanel westpanel = new JPanel();

        //change the size of the north, east, and west panels; they will serve as a border
        northpanel.setPreferredSize(new Dimension(500, 40));
        eastpanel.setPreferredSize(new Dimension(40, 400));
        westpanel.setPreferredSize(new Dimension(40, 400));

        //adding contents to the southpanel, it will contain one button
        southpanel.setLayout(new BoxLayout(southpanel, BoxLayout.X_AXIS));
        southpanel.setPreferredSize(new Dimension(500, 40));
        southpanel.add(Box.createHorizontalGlue());
        southpanel.add(clearButton);
        southpanel.add(Box.createHorizontalGlue());

        System.out.println("Created GUI on EDT? "+
                SwingUtilities.isEventDispatchThread());
         //create the frame
        JFrame demoFrame = new JFrame("Drawing Demo");

        //set up the frame
        demoFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        demoFrame.setLayout(new BorderLayout());
        demoFrame.setSize(600,500);//originally 500,400
        demoFrame.setMinimumSize(new java.awt.Dimension(600, 500));
        demoFrame.setVisible(true);


        //draw1 is made final because it is accessed in the clear button action performed code
        final DrawingPanel draw1 = new DrawingPanel();

        demoFrame.add(draw1, "Center");
        demoFrame.add(southpanel, "South");
        demoFrame.add(northpanel, "North");
        demoFrame.add(eastpanel, "East");
        demoFrame.add(westpanel, "West");

        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                         draw1.clearButtonActionPerformed(evt);
            }
        });

}





}